package Dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import entity.*;

public class ListDao {
	public int updateEvent(String oldName,String title){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "update list set title = ? where title = ? ";
		try{
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, title);
			pstmt.setString(2, oldName);
			
			return pstmt.executeUpdate();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			DB.closeConn(conn, pstmt, null);
		}		
	}
	public int updateType(Event event){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "update list set title = ?,start = ?,end = ?,className = ? where id = ?";
		try{
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, event.getTitle());
			pstmt.setString(2, event.getStart());
			pstmt.setString(3, event.getEnd());
			pstmt.setString(4, event.getClassName());
			pstmt.setInt(5, event.getId());
			
			return pstmt.executeUpdate();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			DB.closeConn(conn, pstmt, null);
		}		
	}
	public int addEvent(Event event){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "insert into list(Id,title,start,end,className) values(?,?,?,?,?) ";
		try{
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, event.getId());
			pstmt.setString(2, event.getTitle());
			pstmt.setString(3, event.getStart());
			pstmt.setString(4, event.getEnd());
			pstmt.setString(5, event.getClassName());
			return pstmt.executeUpdate();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			DB.closeConn(conn, pstmt, null);
		}		
	}
	public int findNum(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet result = null;
		String sql = "select max(id) as count from list";
		conn = DB.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			result = pstmt.executeQuery();
			while(result.next()){
				int num = result.getInt("count");
				return num;
			}		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return 0;
	}
	
	public int findEvent(String className){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet result = null;
		String sql = "select count(className) as count from list where className=?";
		try{
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, className);
			result = pstmt.executeQuery();
			while(result.next()){
				int num = result.getInt("count");
				return num;
			}
			return 0;
			
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}finally{
			DB.closeConn(conn, pstmt, null);
		}		
	}
	
	public List<Event> findAll(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet result = null;
		String sql = "select * from list";		
		try{
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			result = pstmt.executeQuery();
			List<Event> events = new ArrayList<>();
			
			while(result.next()){
				Event event = new Event();			
				event.setId(result.getInt("Id"));
				event.setTitle(result.getString("title"));
				event.setStart(result.getString("start"));
				event.setEnd(result.getString("end"));
				event.setClassName(result.getString("className"));				
				events.add(event);
			}	
			return events;			
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			DB.closeConn(conn, pstmt, null);
		}	
	}
	
	public int deleteEvent(String title,String start){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "delete from list where title = ? and start = ?";
		try {
			conn = DB.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, title);
			pstmt.setString(2, start);
			return pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DB.closeConn(conn, pstmt, null);
		}	
		return 0;
	}
	
}
